#!/usr/bin/env python

import wx

import resource as r
import __form as form

class Form(wx.FlexGridSizer):
    '''Form class
    
    @author Julot <andy.julot@gmail.com>
    @since Thursday, February 23, 2012. 12:58 PM'''
    
    def __init__(self, parent):
        self._parent = parent
        spacer = r.dimen.spacer
        super(Form, self).__init__(cols=2, vgap=spacer, hgap=spacer)
        self.AddGrowableCol(1)
        self.AddGrowableRow(5)
        self.AddGrowableRow(7)
        
        labelFlag = wx.ALIGN_CENTER_VERTICAL
        
        c = r.string.view.contacts.name
        label = wx.StaticText(parent, label='%s:' % c)
        self.Add(label, flag=labelFlag)
        
        self.name = wx.TextCtrl(parent)
        self.Add(self.name, 0, wx.GROW)
        
        c = r.string.view.contacts.address
        label = wx.StaticText(parent, label='%s:' % c)
        self.Add(label, flag=labelFlag)
        
        self.address = wx.TextCtrl(parent)
        self.Add(self.address, 0, wx.GROW)
        
        c = r.string.view.contacts.city
        label = wx.StaticText(parent, label='%s:' % c)
        self.Add(label, flag=labelFlag)
        
        self.city = wx.TextCtrl(parent)
        self.Add(self.city, 0, wx.GROW)
        
        c = r.string.view.contacts.province
        label = wx.StaticText(parent, label='%s:' % c)
        self.Add(label, flag=labelFlag)
        
        self.province = wx.TextCtrl(parent)
        self.Add(self.province, 0, wx.GROW)
        
        c = r.string.view.contacts.phone
        label = wx.StaticText(parent, label='%s:' % c)
        self.Add(label, flag=labelFlag)
        
        self.phone = wx.TextCtrl(parent, style=wx.TE_PROCESS_ENTER)
        self.phoneToolBar = form.ComboBoxToolBar(parent)
        
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.phone, 1, wx.GROW)
        sizer.AddSpacer(spacer)
        sizer.Add(self.phoneToolBar)
        
        self.Add(sizer, 0, wx.GROW)
        
        self.Add((1, 1))
        
        self.phones = wx.ListBox(parent)
        self.Add(self.phones, 1, wx.GROW)
        
        c = r.string.view.contacts.group
        label = wx.StaticText(parent, label='%s:' % c)
        self.Add(label, flag=labelFlag)
        
        self.group = wx.ComboBox(parent, style=wx.TE_PROCESS_ENTER)
        self.groupToolBar = form.ComboBoxToolBar(parent)
        
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.group, 1, wx.GROW)
        sizer.AddSpacer(spacer)
        sizer.Add(self.groupToolBar)
        
        self.Add(sizer, 0, wx.GROW)
        
        self.Add((1, 1))
        
        self.groups = wx.ListBox(parent)
        self.Add(self.groups, 1, wx.GROW)
        
        self.bind()
        
    def bind(self):
        view = self._parent
        
        view.Bind(wx.EVT_TEXT, self._changeAddPhone, self.phone)
        view.Bind(wx.EVT_TEXT_ENTER, self._addPhone, self.phone)
        view.Bind(wx.EVT_LISTBOX, self._changeRemovePhone, self.phones)
        view.Bind(wx.EVT_MENU, self._addPhone, self.phoneToolBar.add.tool)
        view.Bind(wx.EVT_MENU, self._removePhone, self.phoneToolBar.remove.tool)
        view.Bind(wx.EVT_MENU, self._clearPhone, self.phoneToolBar.clear.tool)

        view.Bind(wx.EVT_TEXT, self._changeAddGroup, self.group)
        view.Bind(wx.EVT_TEXT_ENTER, self._addGroup, self.group)
        view.Bind(wx.EVT_LISTBOX, self._changeRemoveGroup, self.groups)
        view.Bind(wx.EVT_MENU, self._addGroup, self.groupToolBar.add.tool)
        view.Bind(wx.EVT_MENU, self._removeGroup, self.groupToolBar.remove.tool)
        view.Bind(wx.EVT_MENU, self._clearGroup, self.groupToolBar.clear.tool)

    def _changeAddPhone(self, event):
        self.phoneToolBar.add.enable(bool(event.String))
        event.Skip()
    
    def _changeRemovePhone(self, event):
        self.phoneToolBar.remove.enable(bool(event.String))
        event.Skip()
    
    def _addPhone(self, event):
        items = [x.strip() for x in self.phone.Value.split(',')]
        items = set(items) - set(self.phones.Strings)
        if items:
            self.phones.AppendItems(list(items))
        self.phoneToolBar.add.disable()
        self.phone.Value = ''
        self.phone.SetFocus()
        event.Skip()
    
    def _removePhone(self, event):
        self.phone.Value = self.phones.GetString(self.phones.Selection)
        self.phones.Delete(self.phones.Selection)
        self.phoneToolBar.remove.disable()
        event.Skip()
    
    def _clearPhone(self, event):
        self.phoneToolBar.remove.disable()
        self.phones.Clear()
        event.Skip()
    
    def _changeAddGroup(self, event):
        self.groupToolBar.add.enable(bool(event.String))
        event.Skip()
    
    def _changeRemoveGroup(self, event):
        self.groupToolBar.remove.enable(bool(event.String))
        event.Skip()
    
    def _addGroup(self, event):
        items = [x.strip() for x in self.group.Value.split(',')]
        items = set(items) - set(self.groups.Strings)
        if items:
            self.groups.AppendItems(list(items))
        self.groupToolBar.add.disable()
        self.group.Value = ''
        self.group.SetFocus()
        event.Skip()
    
    def _removeGroup(self, event):
        self.group.Value = self.groups.GetString(self.groups.Selection)
        self.groups.Delete(self.groups.Selection)
        self.groupToolBar.remove.disable()
        event.Skip()
    
    def _clearGroup(self, event):
        self.groupToolBar.remove.disable()
        self.groups.Clear()
        event.Skip()
    
    _model = None
    
    @property
    def model(self):
        self._model.name = self.name.Value
        self._model.address = self.address.Value
        self._model.city = self.city.Value
        self._model.province = self.province.Value
        self._model.phones = self.phones.Strings
        self._model.groups = self.groups.Strings
        return self._model
    
    @model.setter
    def model(self, value):
        self._model = value
        
        if not value.id:
            self.name.Value = ''
            self.address.Value = ''
            self.phones.Clear()
        else:
            self.name.Value = value.name or ''
            self.address.Value = value.address or ''
            self.city.Value = value.city or ''
            self.province.Value = value.province or ''
            self.phones.Clear()
            if value.phones:
                self.phones.AppendItems(value.phones)
            self.groups.Clear()
            if value.groups:
                self.groups.AppendItems(value.groups)
        self.phoneToolBar.add.disable()
        self.phoneToolBar.remove.disable()
        self.groupToolBar.add.disable()
        self.groupToolBar.remove.disable()